package com.xci.sys.api;

import com.xci.core.annotation.Authorize;
import com.xci.core.internal.Const;
import com.xci.core.base.PrimaryKeyValue;
import com.xci.core.base.RestMessage;
import com.xci.core.helper.ExcelHelper;
import com.xci.core.base.ApiController;
import com.xci.sys.entity.SysDept;
import com.xci.sys.filter.DeptFilter;
import com.xci.sys.service.DeptService;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 机构接口
 *
 * @author 吕艳阳
 */
@ApiSort(3)
@Api(tags = "机构接口")
@Authorize
@RestController
@RequestMapping(value = "/api/sys/dept", produces = Const.P_JSON)
public class DeptApiController extends ApiController {
    @Resource
    private DeptService deptService;//机构服务

    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "新建机构")
    @Authorize(code = "sys.dept.insert")
    @PostMapping("/insert")
    public RestMessage insert(@RequestBody SysDept entity) {
        return deptService.insert(entity);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "修改机构")
    @Authorize(code = "sys.dept.update")
    @PostMapping("/update")
    public RestMessage update(@RequestBody SysDept entity) {
        return deptService.update(entity);
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "修改机构状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "机构主键字符串", required = true),
            @ApiImplicitParam(name = "status", value = "机构状态", required = true)
    })
    @Authorize(code = "sys.dept.updateStatus")
    @PostMapping("/updateStatus")
    public RestMessage updateStatus(String ids, Boolean status) {
        return deptService.updateStatus(ids, status);
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "批量修改字段值")
    @PostMapping("/batchUpdate")
    public RestMessage batchUpdate(@RequestBody List<PrimaryKeyValue> keyValues) {
        return deptService.batchUpdate(keyValues);
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "删除机构")
    @ApiImplicitParam(name = "ids", value = "机构主键字符串", required = true)
    @Authorize(code = "sys.dept.delete")
    @PostMapping("/delete")
    public RestMessage delete(String ids) {
        return deptService.delete(ids);
    }

    @ApiOperationSupport(order = 6)
    @ApiOperation(value = "检查机构编码是否存在")
    @ApiImplicitParam(name = "code", value = "机构编码", required = true)
    @PostMapping("/existByCode/{code}")
    public RestMessage existByCode(@PathVariable String code) {
        return RestMessage.result(deptService.existByCode(code, null));
    }

    @ApiOperationSupport(order = 7)
    @ApiOperation(value = "检查机构名称是否存在")
    @ApiImplicitParams({
             @ApiImplicitParam(name = "parentId", value = "机构上级主键", required = true),
             @ApiImplicitParam(name = "name", value = "机构名称", required = true)
    })
    @PostMapping("/existByName/{parentId}/{name}")
    public RestMessage existByName(@PathVariable String parentId, @PathVariable String name) {
        return RestMessage.result(deptService.existByName(parentId, name, null));
    }

    @ApiOperationSupport(order = 8)
    @ApiOperation(value = "查询单个机构")
    @ApiImplicitParam(name = "id", value = "机构主键", required = true)
    @PostMapping("/selectById/{id}")
    public RestMessage<SysDept> selectById(@PathVariable String id) {
        return RestMessage.success(deptService.selectById(id));
    }

    @ApiOperationSupport(order = 9)
    @ApiOperation(value = "根据编码查询单个机构")
    @ApiImplicitParam(name = "code", value = "机构编码", required = true)
    @PostMapping("/selectByCode/{code}")
    public RestMessage<SysDept> selectByCode(@PathVariable String code) {
        return RestMessage.success(deptService.selectByCode(code));
    }

    @ApiOperationSupport(order = 10)
    @ApiOperation(value = "获取节点的直系子节点数")
    @ApiImplicitParam(name = "id", value = "机构主键", required = true)
    @PostMapping("/selectChildCount/{id}")
    public RestMessage selectChildCount(@PathVariable String id) {
        return RestMessage.success(deptService.selectChildCount(id));
    }

    @ApiOperationSupport(order = 11)
    @ApiOperation(value = "查询机构列表")
    @PostMapping("/selectList")
    public RestMessage<List<SysDept>> selectList(DeptFilter filter) {
        return RestMessage.success(deptService.selectList(filter));
    }

    @ApiOperationSupport(order = 12)
    @ApiOperation(value = "导出机构列表")
    @ApiImplicitParam(name = "fieldNames", value = "导出字段名字符串")
    @Authorize(code = "sys.dept.export")
    @PostMapping(value = "/export", produces = {Const.P_OCTET, Const.P_JSON})
    public void export(DeptFilter filter) {
        ExcelHelper.exportWeb(deptService.selectList(filter), SysDept.class, "系统机构列表");
    }

    @ApiOperationSupport(order = 14)
    @ApiOperation(value = "刷新机构缓存")
    @Authorize(code = "sys.dept.refreshCache")
    @PostMapping(value = "/refreshCache")
    public RestMessage refreshCache() {
        deptService.refreshCache();
        return RestMessage.success();
    }
}